<?php defined('SYSPATH') or die('No direct access');
/**
 * 评价升级，添加信誉积分
 *
 * @package
 * @category
 * @email miaodabiao@163.com
 * @version
 */

class Cli_Rate extends Cli
{
	public function main()
	{
		$start = microtime(TRUE);

		$select = DB::select()->from('order_rates');

		// 实例化迭代器
        $iterator = new Db_Iterator($select, 'default', 10);

        $total = $iterator->total();

	 	if($total == 0)
        {
        	CLI::output( "没有数据要更新");
        	die;
        }

        $start = microtime(TRUE);

		// 循环取出数据
		while ($iterator->valid())
        {
            // 遍历数据
			foreach ($iterator->current() as $row)
            {
				if ($row['buyer_id'] == 0)
            	{
            		// 匿名评价
            		$is_anonymity = 1;

            		$buyer_id = DB::select('member_id')
	            		->from('orders')
	            		->where('order_id', '=', $row['order_id'])
	            		->fetch_one();
            	}
            	else
            	{
            		// 非匿名
            		$is_anonymity = 0;

            		$buyer_id = $row['buyer_id'];
            	}

                // 插入店铺动态评分
	            if( ! DB::select()->from('shop_rates')->where('order_id', '=', $row['order_id'])->count_all())
	            {
	            	//不存在店铺动态评分
	            	$insert = array(
	            		'shop_id'=>$row['shop_id'],
	            		'order_id'=>$row['order_id'],
	            		'member_id'=>$buyer_id,
	            		'des_score'=>$row['des_score'],
	            		'service_score'=>$row['service_score'],
	            		'express_score'=>$row['express_score'],
	            		'created_time'=>$row['comment_time'],
	            	);

	            	DB::insert('shop_rates', array_keys($insert))->values(array_values($insert))->execute();
	            }

            	$nickname = DB::select('nickname')
	            	->from('members')
	            	->where('member_id', '=', $buyer_id)
	            	->fetch_one();

	            $shop = DB::select('shop_name', 'member_id')
	            	->from('shops')
	            	->where('shop_id', '=', $row['shop_id'])
	            	->fetch_row();


	            $shop_name = $shop['shop_name'];

	            $seller_id = $shop['member_id'];

	            $order_goods = DB::select()
		            ->from('order_goods')
		            ->where('order_id', '=', $row['order_id'])
		            ->where('goods_id', '=', $row['goods_id'])
		            ->fetch_row();

		       $order_end_time = DB::select('end_time')
		       		->from('orders')
		            ->where('order_id', '=', $row['order_id'])
		            ->fetch_one();


	           // 买家评价
	           $data = array(
	                'order_id'=>$row['order_id'],
	                'buyer_id'=>$buyer_id,
	                'nickname'=>$nickname,
	                'shop_id'=>$row['shop_id'],
	                'shop_name'=>$shop_name,
	                'goods_id'=>$row['goods_id'],
	                'goods_title'=>$order_goods['goods_title'],
	                'prop_str'=>$order_goods['prop_str'],
	                'price'=>$order_goods['price'],
	                'buyer_comment_score'=>1,
	                'buyer_comment'=>$row['comment'],
	                'buyer_comment_time'=>$row['comment_time'],
	                'buyer_deleted'=>$row['deleted'],
	                'is_anonymity'=>$is_anonymity,
	            	'buyer_flag'=>1,
				);

				// 超期评价时间
				$timelimit = 15*3600*24;

				$seller_comment_time = $need_seller_comment = 0;

				if(empty($order_end_time))
				{
					//历史数据没有记录确认收货时间的评价
					$need_seller_comment = 1;

					$seller_comment_time = $row['comment_time'] + 1*3600*24 + rand(100,999);
				}
				elseif (time() - $order_end_time > $timelimit)
		        {
		        	// 超期没有评价
		        	$need_seller_comment = 1;

					$seller_comment_time = $order_end_time + $timelimit;
		        }

		        if($need_seller_comment)
		        {
		        	//商户评分
		        	$seller_comment = array(
	           			'seller_comment_score'=>1,
	                	'seller_comment'=>'超级好买家，赞，期待再次光临！',
	                	'seller_comment_time'=>$seller_comment_time,
	                	'seller_deleted'=>0,
	           			'seller_flag'=>1,
		           	);

	           		// 更新商户信誉积分,评分次数
	           		DB::update('members')
		           		->set(array('credit_score'=>DB::expr('credit_score+1'), 'good_num'=>DB::expr('good_num+1')))
		           		->where('member_id', '=', $seller_id)
		           		->execute();

		           	// 更新orders表 seller_rate状态
		           	DB::update('orders')
		           		->set(array('seller_rate'=>1))
		           		->where('order_id', '=', $row['order_id'])
		           		->execute();


	           		$data = array_merge($data, $seller_comment);
		        }


				DB::insert('order_rates_copy', array_keys($data))
	                ->values(array_values($data))
	                ->execute();

				// 更新个人信誉积分,评分次数
				DB::update('members')
		           	->set(array('credit_score'=>DB::expr('credit_score+1'), 'good_num'=>DB::expr('good_num+1')))
		           	->where('member_id', '=', $buyer_id)
		           	->execute();


	            }


	            echo ">";

	            $iterator->next();
        }
        //使用新表
		mysql_query("rename table order_rates to order_rates_bak;");
		mysql_query("rename table order_rates_copy to order_rates;");
        Cli::output("共用时".(round(time() - $start, 3))." 秒");
	}

}